<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : shop.class.php   FILE_PATH : \\classes\application\shop.class.php
 * 商铺操作类
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Wed Sep 05 22:58:35 CST 2007
 */

defined('Base_Use_Sign') or die("this is invalid system!please contact <a href='http://www.shopnc.net'>www.shopnc.net</>.");

class ShopClass extends FrameWork {
	
	/**
	 * 填写店铺信息（新增或修改）
	 *
	 * @param var[] $input_param 
	 * @return boolen
	 */
	function operateShop($input_param){
		$valueArray = array();
		if ($input_param['hideMemberId'] != ""){
			$valueArray['member_id'] = $input_param['hideMemberId'];/*会员编号*/
		}
		if ($input_param['txtShopName'] != ""){
			$valueArray['shop_name'] = $input_param['txtShopName'];/*商铺名称*/
		}
		if ($input_param['slcShopClass'] != ""){
			$valueArray['shop_class'] = $input_param['slcShopClass'];/*商铺类别*/
		}
		if ($input_param['txtSaleRange'] != ""){
			$valueArray['sale_range'] = $input_param['txtSaleRange'];/*经营范围*/
		}
//		if ($input_param['txtProvince'] != "") {
//			$valueArray['shop_province'] = $input_param['txtProvince'];/*省*/
//		}
//		if ($input_param['txtCity'] != "") {
//			$valueArray['shop_city'] = $input_param['txtCity'];/*市*/
//		}
		if ($input_param['txtShopIntro'] != ""){
			$valueArray['shop_intro'] = Common::replacebr($input_param['txtShopIntro']);/*商铺介绍*/
		}
		if ($input_param['fileShopLogo'] != ""){
			$valueArray['shop_pic'] = $input_param['fileShopLogo'];/*商铺图片*/
		}
		if ($input_param['txtProclamation'] != ""){
			$valueArray['proclamation'] = $input_param['txtProclamation'];/*商铺公告*/
		}
		if ($input_param['shop_domain'] != ""){
			$valueArray['shop_domain'] = $input_param['shop_domain'];/*商铺二级域名*/
		}
		if ($input_param['templates'] != ""){
			$valueArray['templates'] = $input_param['templates'];/*模板类别标识*/
		}
		//商铺样式
		if ($input_param['sel_type'] == 1){//自定义模板
			if ($input_param['shop_style'] != ''){
				$valueArray['shop_style'] = $input_param['shop_style'];
			}
		}else {//现有模板
			if ($input_param['rdoStyle'] != ''){
				$v = explode("|||",$input_param['rdoStyle']);
				if (is_array($v)){
					$valueArray['shop_style'] = $v[0];
				}
			}
		}
		if ($input_param['hideShopId'] != ""){
			$valueArray['shop_id'] = $input_param['hideShopId'];/*门店ID*/
		}
		if ($input_param['fileShopBanner'] != ""){
			$valueArray['shop_banner_pic'] = $input_param['fileShopBanner'];//shopbanner图片
		}
		if ($input_param['area_id'] != ''){
			$valueArray['shop_area_id'] = $input_param['area_id'];//店铺地区ID
		}
		
		//判断是否拥有店铺
		if ($input_param['hideMemberId'] != ""){
			$is_have = $this->isHaveShop($input_param['hideMemberId']);
		}
		else{
			$is_have = true;
		}
		if ($is_have == false){
			if ($input_param['ischeck'] == "0" || $input_param['ischeck'] == "1"){
				$valueArray['ischeck'] = 1 - intval($input_param['ischeck']);
			}
			return $this->_addShop($valueArray);
		}else{
			//判断如果当前审核状态为关闭的话，则更新状态为申请
			if ($input_param['now_check'] == '2'){
				$valueArray['ischeck'] = '0';
			}
			return $this->_updateShop($valueArray);
		}
	}
	
	/**
	 * 增加店铺信息
	 *
	 * @param var[] $input_param
	 * @return boolen
	 */
	function _addShop($input_param){
		$input_param['practice_time'] = time();
		$insert_rs = $GLOBALS['db']->InsertRow($input_param, 'shop_info', 'shop_id');
		return $insert_rs;
	}

	/**
	 * 修改店铺信息
	 *
	 * @param var[] $input_param
	 * @return boolen
	 */
	function _updateShop($input_param){
		$update_rs = $GLOBALS['db']->UpdateRow($input_param["shop_id"],$input_param, 'shop_info', 'shop_id');
		return $update_rs;
	}
	
	/**
	 * 删除图片
	 * @author     
	 * @param  
	 * @return bool
	 */
	function delPic($shop_id){
		$valueArray = array();
		$valueArray['shop_id'] = $shop_id;
		$valueArray['shop_pic'] = '';
		return $this->_updateShop($valueArray);
	}
	
	/**
	 * 修改店铺信息
	 *
	 * @param var[] $input_param
	 * @return boolen
	 */
	function _updateShopParentId($parentId,$new_id){
		$value_array = array();
		$value_array['shop_class'] = $new_id;
		$result = $GLOBALS['db']->UpdateRow($parentId,$value_array,'shop_info','shop_class');
		return $result;
	}
	
	/**
	 * 得到某一店铺信息
	 *
	 * @param int $input_id
	 * @param string $genre
	 * @param string $fields 所需要的字段
	 * @return array $shop_row 	返回商铺数组
	 */
	function getOneShop($input_id,$genre="0",$fields="*"){
		if ($input_id == ""){
			return false;
		}
		if ($genre == "0"){
			$shop_row = $GLOBALS['db']->GetJoinList($page,array('shop_info','member','member_extend'),"left join",array("shop_info.member_id=member.member_id","shop_info.member_id=member_extend.member_id"),array("*","login_name,regist_time","province,city,sell_product_count,buy_score,sale_score"),"and shop_info.shop_id='" . intval($input_id) . "'");
			$shop_row = $shop_row[0];
		}else if($genre == "1"){
			$shop_row = $GLOBALS['db']->GetOneRow($input_id,'shop_info','shop_id',$fields);
		}
		if (is_array($shop_row)){
			$shop_array['shop_intro'] = $shop_array['shop_intro'];
			return $shop_row;
		}else{
			return false;
		}
	}
	
	/**
	 * 根据会员编号得到店铺信息
	 *
	 * @param int $input_id
	 * @param string $genre
	 * @param string $fields 所需要的字段
	 * @return array $shop_row 	返回商铺数组
	 */
	function getOneShopByMemeberId($input_id,$genre="0",$fields="*"){
		if ($input_id == ""){
			return false;
		}
		if ($genre == "0"){
			$shop_row = $GLOBALS['db']->GetJoinList($page,array('shop_info','member','member_extend'),"left join",array("shop_info.member_id=member.member_id","shop_info.member_id=member_extend.member_id"),array("*","login_name,regist_time","province,city,area_id"),"and shop_info.member_id='" . $input_id . "'");
			$shop_row = $shop_row[0];
		}else if($genre == "1"){
			$shop_row = $GLOBALS['db']->GetOneRow($input_id,'shop_info','member_id',$fields);
		}
		if (is_array($shop_row)){
			return $shop_row;
		}else{
			return false;
		}
	}

	/**
     * 得到商铺列表信息
     *
     * @param object $obj_condition
     * @param object $obj_page
     * @return array $shop_array 	返回商铺数组
     */
	function getShopList($obj_condition, &$obj_page){
		$condition = $this->setCondition($obj_condition);
		$shop_array = $GLOBALS['db']->GetJoinList(
			$obj_page,
			array(
				"shop_info",
				"member",
				"member_extend",
				'area'
			),
			"left join",
			array(
				"shop_info.member_id=member.member_id",
				"member.member_id=member_extend.member_id",
				"shop_info.shop_area_id=area.area_id"
			),
			array(
				'shop_id,ischeck,audit_state,shop_pic,identity_card_copy_up,
				identity_card_copy_back,license_copy,shop_name,sale_range,
				shop_class,practice_time,member_id,shop_index,shop_province,
				shop_city,shop_domain,shop_area_id,if_del',
				'member_id,login_name',
				'sell_product_count,province,city,sale_score,area_id,shop_availability_time,
				product_number,personal_certify',
				'area_name'
			),
			$condition,
			0
		);
		return $shop_array;
	}
	
	/**
	 * getShopNumInAll
	 *
	 * @param unknown_type $key
	 * @param unknown_type $obj_condition
	 * @param unknown_type $obj_page
	 * @return unknown
	 */
	function getShopNumInAll() {
		$total = $GLOBALS['db']->CountRows('shop_info', "ischeck != '2'");
		return $total;
	}
	/**
	 * 判断某个会员是否拥有店铺
	 *
	 * @param int $input_id 会员ID
	 */
	function isHaveShop($input_id){
		$shop_row = $GLOBALS['db']->GetOneRow($input_id,'shop_info','member_id');
		if ($shop_row['member_id'] == ''){
			return false;
		}
		return true;
	}

	/**
	 * 根据会员名称得到商铺ID
	 *
	 * @param string $member_name
	 * @return var
	 */
	function getShopID($member_name,$field_name = "login_name",$ischeck="0"){
		if (trim($member_name) != ""){
			$shop_row = $GLOBALS['db']->GetJoinList($obj_page,array("member","shop_info"),"inner join",array("member.member_id=shop_info.member_id"),array("*","shop_id"),"and member." . $field_name . "='" . $member_name . "'");
			if ($shop_row[0]['shop_id'] == ''){
				return false;
			}else{
				return $shop_row[0]['shop_id'];
			}
		}
		return false;
	}

	/**
	 * 商铺是否存在
	 *
	 * @param int $input_id
	 * @return boolen
	 */
	function isExistShop($input_id){
		if ($input_id == '' || $input_id<=0){
			return false;
		}
		$shop_row = $GLOBALS['db']->GetOneRow($input_id,'shop_info','shop_id');
		if ($shop_row['shop_id'] == ''){
			return false;
		}
		return true;
	}

	/**
	 * 组成查询条件字符串
	 *
	 * @param var[] $arr
	 * @return string
	 */
	function setCondition($arr){
		$str = "";

		if ($arr["txtShopName"] != ""){
			$str .= " and shop_info.shop_name like '%" . $arr["txtShopName"] . "%'";
		}
		if ($arr["keyword"] != ""){
			$str .= " and shop_info.shop_name like '%" . $arr["keyword"] . "%'";
		}
		if ($arr["txtSaleRange"] != ""){
			$str .= " and shop_info.sale_range like '%" . $arr["txtSaleRange"] . "%'";
		}
		if ($arr["ischeck"] != ""){
			$str .= " and shop_info.ischeck = '" . $arr["ischeck"] . "'";
		}
		if ($arr["audit_state"] != ""){
			$str .= " and shop_info.audit_state = '" . $arr["audit_state"] . "'";
		}
		if ($arr["no_audit_state"] != ""){
			$str .= " and shop_info.audit_state != '" . $arr["no_audit_state"] . "'";
		}
		//类别
		if ($arr["slcShopClass"] != ""){
			$str .= " and (shop_info.shop_class ='" . $arr["slcShopClass"] . "'";
			/*搜索子分类*/
			require_once("shopcategory.class.php");
			$obj_search_cate = new ShopCategoryClass();
			$subcate = $obj_search_cate->getCategory($arr["slcShopClass"],false);
			if (is_array($subcate)){
				$category_sql = "";
				foreach ($subcate as $value){
					$category_sql .= " or shop_info.shop_class = '" . $value['class_id'] . "'";
				}
			}
			unset($obj_search_cate);
			$str .= $category_sql . ")";
		}
		//会员状态
		if ($arr["member_state"] != ""){
			$str .= " and member.member_state = '" . $arr["member_state"] . "'";
		}
		//模糊搜索会员名
		if ($arr["txtMaster"] != ""){
			$str .= " and member.login_name like '%" . $arr["txtMaster"] . "%'";
		}
		/*首页显示*/
		if ($arr['shop_index'] != "") {
			$str .= " and shop_info.shop_index = '". $arr['shop_index'] ."'";
		}
		/*地区*/
		/**
			 * 判断管理员所在权限组
			 */
		if ($_SESSION['s_admin']['id'] != ''){
			/**
				 * 创建管理员对象
				 */
			if (!is_object($this->obj_sys_member)) {
				require_once("system_power.class.php");
				$this->obj_sys_member = new SystemPowerClass();
			}
			$sys_member_info = $this->obj_sys_member->getSysMemberById($_SESSION['s_admin']['id']);
			$sys_group_info  = $this->obj_sys_member->getSysGroupById($sys_member_info['sys_group_id']);
			if ($sys_group_info['sys_group_area_id']){
				$arr['shop_area_id'] = $sys_group_info['sys_group_area_id'];
			}
		}
		if ($arr['shop_area_id'] != "") {
			//取该地区下的所属地区
			if (!is_object($obj_area)){
				require_once("area.class.php");
				$obj_area = new AreaClass();
			}
			$condition_area['area_path_like'] = ','.$arr['shop_area_id'].',';
			$area_list = $obj_area->getArea($condition_area);
			if (is_array($area_list)){
				foreach ($area_list as $k => $v){
					$area_line .= " or shop_info.shop_area_id = '" . $v['area_id'] . "'";
				}
			}
			$str .= " and (shop_info.shop_area_id = '" . $arr['shop_area_id'] . "'";
			$str .= $area_line;
			$str .= ')';
			unset($obj_area,$area_line);
		}
		//店铺使用时间
		if ($arr['expire_shop_availability_time'] != ''){
			$str .= " and member_extend.shop_availability_time <= '" . $arr['expire_shop_availability_time'] . "'";
		}
		//店铺删除状态
		if ($arr['if_del'] != ''){
			$str .= " and shop_info.if_del = '" . $arr['if_del'] . "'";
		}else {
			$str .= " and shop_info.if_del = '0'";
		}
		
		if ($arr["order"] == 1){
			$str .= " order by shop_info.shop_id desc";
		}else if($arr["order"] == 2){

			//$str .= " order by shop_info.shop_id desc";
		}else if($arr["order"] == 3){
			if ($arr["order_by"] != ''){
				$str .= " order by ".$arr["order_by"];
			}
		}
		
		return $str;
	}

	/**
	 * 设置商铺的审核状态
	 *
	 * @param int $shop_id
	 * @param int $state
	 * @return boolen
	 */
	function checkShop($shop_id,$state){
		$arr = array();
		$arr['shop_id'] = $shop_id;
		$arr['ischeck'] = $state;
		return $this->_updateShop($arr);
	}
	
	/**
	 * 设置实体商铺的审核状态
	 *
	 * @param int $shop_id
	 * @param int $state
	 * @return boolen
	 */
	function checkShopEntity($shop_id,$state){
		$arr = array();
		$arr['shop_id']     = $shop_id;
		$arr['audit_state'] = $state;
		return $this->_updateShop($arr);
	}

	/**
	 * 生成商铺列表数组方法
	 *
	 * @param object $record_count 获取的条数
	 * @param object $condition	 	条件
	 * @return array $shop_array 返回商品列表数组
	 */
	function getSomeShopList($record_count,$condition = ""){
		$condition = $this->setCondition_no_tab($condition);
		$shop_array = $GLOBALS['db']->GetList($condition . " order by practice_time desc limit 0," . $record_count, $obj_page, "shop_info","shop_id,shop_name,shop_pic,member_id");
		return $shop_array;
	}
	
	/**
	 * 组成查询条件字符串
	 *
	 * @param var[] $arr
	 * @return string
	 */
	function setCondition_no_tab($arr){
		$str = "";

		if (is_array($arr)){
			if ($arr["txtShopName"] != ""){
				$str .= " and shop_name like '%" . $arr["txtShopName"] . "%'";
			}
			if ($arr["keyword"] != ""){
				$str .= " and shop_name like '%" . $arr["keyword"] . "%'";
			}
			if ($arr["txtSaleRange"] != ""){
				$str .= " and sale_range like '%" . $arr["txtSaleRange"] . "%'";
			}
			if ($arr["slcShopClass"] != ""){
				$str .= " and (shop_class ='" . $arr["slcShopClass"] . "'";
				/*搜索子分类*/
				require_once("shopcategory.class.php");
				$obj_search_cate = new ShopCategoryClass();
				$subcate = $obj_search_cate->getCategory($arr["slcShopClass"],false);
				if (is_array($subcate)){
					$category_sql = "";
					foreach ($subcate as $value){
						$category_sql .= " or shop_class = '" . $value['class_id'] . "'";
					}
				}
				unset($obj_search_cate);
				$str .= $category_sql . ")";
			}
			if ($arr["shop_index"] != ""){
				$str .= " and shop_index = '" . $arr["shop_index"] . "'";
			}
			if ($arr["order"] == 1){
				$str .= " order by s.shop_id desc";
			}else if($arr["order"] == 2){
				//$str .= " order by s.shop_id desc";
			}
		}
		return $str;
	}
	
	/**
	 * 更新会员的发布商品数量，橱窗推荐数量
	 *
	 * @param int $member_id 会员ID
	 * @param string $genre 更新的类别recommend商铺推荐商品数量
	 * @param var $value 数值
	 * @return boolen
	 */
	function updateStatistics($shop_id,$genre = "recommend",$value = 0){
		$shop_array = array();
		switch($genre){
			case "recommend":
				$shop_array['recommend_product_count']  = $value;  //商铺推荐商品数量
				break;
		}
		$update_rs = $GLOBALS['db']->UpdateRow($shop_id,$shop_array,"shop_info","shop_id");
		return $update_rs;
	}
	
	/**
	 * 删除商店
	 *
	 * @param var $id 会员ID
	 * @return boolen
	 */
	function delShopByMemberId($id){
		if (intval($id) > 0){
			$result = $GLOBALS['db']->DeleOneRow(intval($id),'shop_info','member_id');
			return $result;
		}else {
			return false;
		}
	}
	
	/**
	 * 根据2级域名编号得到店铺信息
	 *
	 * @param int $input_id
	 * @param string $genre
	 * @param string $fields 所需要的字段
	 * @return array $shop_row 	返回商铺数组
	 */
	function getOneShopByDomainId($input_id,$genre="0",$fields="*"){
		if ($input_id == ""){
			return false;
		}
		if ($genre == "0"){
			$shop_row = $GLOBALS['db']->GetJoinList($page,array('shop_info','member','member_extend'),"left join",array("shop_info.member_id=member.member_id","shop_info.member_id=member_extend.member_id"),array("*","login_name,regist_time","province,city,area_id"),"and shop_info.shop_domain='" . $input_id . "'");
			$shop_row = $shop_row[0];
		}else if($genre == "1"){
			$shop_row = $GLOBALS['db']->GetOneRow($input_id,'shop_info','shop_domain',$fields);
		}
		if (is_array($shop_row)){
			return $shop_row;
		}else{
			return false;
		}
	}
	
	/**
	 * 转移类别下的店铺到另一个类别中
	 * @author workflow Develop Team     
	 * @param  $move_array需要转移的ID数组,$to_id转入的类别ID
	 * @return bool
	 */
	function moveShopClass($move_array,$to_id){
		if (intval($to_id) > 0 && !empty($move_array)){
			foreach ($move_array as $k => $v){
				$condition_str[] = "shop_id = '" . $v . "'";
			}
			$condition_str = implode(' or ',$condition_str);
			$value_array = array();
			$value_array['shop_class'] = intval($to_id);
			if (substr(trim($condition_str),0,3) == 'and'){
				$condition_str = substr(trim($condition_str),3);
			}
			$result = $GLOBALS['db']->UpdateRows('shop_info',$value_array,$condition_str);
			unset($condition,$condition_str,$value_array);
			return $result;
		}
		return false;
	}

	/**
	 * 模板内容更新
	 *
	 * @param array 更新内容
	 * @return Boolean 返回布尔类型的结果
	 */
	function updateShopStyle($value_array){
		$valueArray = array();
		$valueArray['shop_id'] = $value_array['shop_id'];
		$valueArray['shop_style'] = $value_array['shop_style'];
		return $this->_updateShop($valueArray);
	}
}
?>